package com.elric.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.elric.mall.common.utils.DateUtils;
import com.elric.mall.common.utils.ExcelFileUtil;
import com.elric.mall.dto.DynamicExportDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 实现功能：通用controller
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-11-02 11:18
 */
@Controller
@Api(tags = "UmsCommonController", description = "通用控制器")
@RequestMapping("/common")
public class UmsCommonController {

    @ApiOperation("动态excel数据导出")
    @RequestMapping(value = "/dynamicExport", method = RequestMethod.POST)
    @ResponseBody
    public void dynamicExport(@RequestBody DynamicExportDto dto, HttpServletResponse response) {
        String name = "未命名";
        if (StrUtil.isNotBlank(dto.getFileName())) {
            name = dto.getFileName();
        }
        String fileName = name+ DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDDHH);
        ExcelFileUtil.toExcelWithXlsx(response, dto.getData(), dto.getHeaders(),fileName);
    }
}
